﻿using CacheManager.Core;
using System;
using Yz.Base;
using Yz.Base.Constants;

namespace Yz.Cache
{
    public class CacheManagerProvider
    {
        private readonly string redisConnstr = CacheConfig.RedisConnStr;
        private readonly int redisDbNum = 0;
        private readonly string redisHost = "";
        private readonly int redisPort = 6379;
        private readonly string redisPwd = "";
        private readonly TimeSpan _timeSpan;
        public CacheManagerProvider(int dbNum, TimeSpan timeSpan)
        {
            _timeSpan = timeSpan;
            redisDbNum = dbNum;
            string[] strRcList = redisConnstr.Split(',');
            foreach (var item in strRcList)
            {
                if (item.Contains(":"))//获得host和port
                {
                    string[] hostPort = item.Split(':');
                    if (hostPort.Length == 2)
                    {
                        redisHost = hostPort[0];
                        redisPort = Convert.ToInt32(hostPort[1]);
                    }
                }
                else //获得pwd
                {
                    string[] hostPort = item.Split('=');
                    if (hostPort.Length == 2 && hostPort[0] == "password")
                    {
                        redisPwd = hostPort[1];
                    }
                }
            }
        }

        public ICacheManager<ReturnList<T>> GetReturnCacheManager<T>() where T : ViewModel
        {
            var manager = GetCacheManager<ReturnList<T>>();
            return manager;
        }
        public ICacheManager<T> GetCacheManager<T>()
        {
            var manager = CacheFactory.Build<T>(CacheConfig.CachePrefixName + CacheConstant.CMCacheName + redisDbNum, GetCacheSetting());
            return manager;
        }
        private Action<ConfigurationBuilderCachePart> GetCacheSetting()
        {
            Action<ConfigurationBuilderCachePart> cacheSetting = settings =>
            {
                string prefixName = CacheConfig.CachePrefixName;
                string redisConfigName = prefixName + CacheConstant.CMRedisName;
                settings
                    .WithUpdateMode(CacheUpdateMode.Up)
                    .WithSystemRuntimeCacheHandle(prefixName + CacheConstant.AppCacheName + redisDbNum)
                    .WithExpiration(ExpirationMode.Sliding, _timeSpan)
                    .And
                    .WithRedisConfiguration(redisConfigName, config =>
                    {

                        config.WithAllowAdmin()
                            .WithDatabase(redisDbNum)
                            .WithEndpoint(redisHost, redisPort)
                            .WithPassword(redisPwd);
                    })
                    .WithMaxRetries(100)
                    .WithRetryTimeout(50)
                    .WithRedisBackplane(redisConfigName)
                    .WithRedisCacheHandle(redisConfigName, true);
            };
            return cacheSetting;
        }
    }
}
